#include "AddressPanel.h"

AddressPanel::AddressPanel(QWidget* parent)
    : QWidget(parent)
    , ip_input(new QLineEdit("localhost", this))
    , port_input(new QLineEdit("12345", this))
    , launch_button(new QPushButton(LAUNCH_TEXT, this))
    , form(new QFormLayout)
    , widget_layout(new QVBoxLayout(this))
{
    form->addRow("IP", ip_input);
    form->addRow("端口", port_input);

    widget_layout->addStretch();
    widget_layout->addLayout(form);
    widget_layout->addStretch();
    widget_layout->addWidget(launch_button);
    widget_layout->addStretch();

    connect(launch_button, SIGNAL(clicked()), this,
        SLOT(onLaunchButtonClicked()));
}

void AddressPanel::onLaunchButtonClicked()
{
    if (launch_button->text() == LAUNCH_TEXT) {
        launch_button->setText(QUIT_TEXT);

        ip_input->setEnabled(false);
        port_input->setEnabled(false);

        emit launched();
    } else if (launch_button->text() == QUIT_TEXT) {
        launch_button->setText(LAUNCH_TEXT);

        ip_input->setEnabled(true);
        port_input->setEnabled(true);

        emit quited();
    }
}

QPair<QString, int> AddressPanel::getAddress()
{
    return { ip_input->text(), port_input->text().toInt() };
}
